# coding:utf-8
import os

root_path = os.path.split(os.path.split(os.path.realpath(__file__))[0])[0]
import sys

sys.path.append(root_path)
import os
import time

import pytest

from utils.common.mkDir import mk_dir
from utils.common.sendEmail import send_email
from config.confManage import dir_manage

project_path = os.path.split(os.path.realpath(__file__))[0]

if ':' in project_path:
    project_path = project_path.replace('\\', '/')
else:
    pass


def run():
    date = time.strftime('%Y-%m-%d')
    localtime = time.strftime('%Y%m%d%H%M%S', time.localtime())
    test_case_path = project_path + dir_manage('${case_dir}$') + dir_manage('${test_name}$')
    #temp地址变量
    temp_path = project_path + dir_manage('${report_xml_dir}$') + "temp/" + localtime + '/'
    #html地址变量
    html_path = project_path + dir_manage('${report_html_dir}$') + date + '/'
    #如果不存在地址路径则创建文件夹
    mk_dir(temp_path)
    mk_dir(html_path)
    #执行命令行
    args = ['-s', '-q', test_case_path, '--alluredir', temp_path]
    pytest.main(args)
    cmd = 'allure generate %s -o %s -c' % (temp_path, html_path)
    os.system(cmd)
    # 发送报告
    send_email(localtime + "测试报告", "http://192.168.1.2:9999")
    # 生成html报告
    os.system(r'allure generate {0} -o {1} '.format(temp_path, html_path))
    # 打开报告服务 并指定端口
    os.system(r'allure serve {0} -p 9999'.format(temp_path))


if __name__ == '__main__':
    run()
